package com.ht.base.system.biz.convert;


import com.ht.base.system.common.pojo.bo.SysUserBO;
import com.ht.base.system.dao.domain.SysUserDO;
import org.base.common.appexception.AppErrorCodeEnum;
import org.base.common.appexception.AppException;
import org.base.common.constans.SystemConstant;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 类SysUserBeanUtil.java的实现描述：工具类
 * 
 * @Author hdx
 * @Date 2023年05月13日
 */
public class SysUserBeanUtil {

    private SysUserBeanUtil(){
    }

    public static SysUserDO sysUserBOToDO(SysUserBO srcObj) {
        if (srcObj == null) {
            return null;
        }
        SysUserDO targetObj = new SysUserDO();
        targetObj.setId(srcObj.getSysUserId());//id
        targetObj.setLoginNo(srcObj.getLoginNo());//登录账号
        targetObj.setUserName(srcObj.getUserName());//userName
        targetObj.setNickName(srcObj.getNickName());//用户昵称
        targetObj.setSysDeptId(srcObj.getSysDeptId());//部门ID
        targetObj.setPhoneNo(srcObj.getPhoneNo());//手机号码
        targetObj.setUserType(srcObj.getUserType());//用户类型（00系统用户）
        targetObj.setPassWdMd(srcObj.getPassWdMd());//passWdMd
        targetObj.setPassWd(srcObj.getPassWd());//passWd
        targetObj.setEmail(srcObj.getEmail());//用户邮箱
        targetObj.setUserStatus(srcObj.getUserStatus());
        targetObj.setLoginIp(srcObj.getLoginIp());//最后登录IP
        targetObj.setLoginDate(srcObj.getLoginDate());//最后登录时间
        targetObj.setCreateTime(srcObj.getCreateTime());//创建时间
        targetObj.setUpdateTime(srcObj.getUpdateTime());//最近修改时间
        targetObj.setDelFlag(srcObj.getDelFlag());//是否删除（0 未删除，1 已删除）
        targetObj.setDeptName(srcObj.getDeptName());//部门名称
        targetObj.setRemark(srcObj.getRemark());
        targetObj.setSex(srcObj.getSex());

        return targetObj;
    }

    public static SysUserBO sysUserDOToBO(SysUserDO srcObj) {
        if (srcObj == null) {
            return null;
        }
        SysUserBO targetObj = new SysUserBO();
        targetObj.setSysUserId(srcObj.getId());//id
        targetObj.setLoginNo(srcObj.getLoginNo());//登录账号
        targetObj.setUserName(srcObj.getUserName());//userName
        targetObj.setNickName(srcObj.getNickName());//用户昵称
        targetObj.setSysDeptId(srcObj.getSysDeptId());//部门ID
        targetObj.setPhoneNo(srcObj.getPhoneNo());//手机号码
        targetObj.setUserType(srcObj.getUserType());//用户类型（00系统用户）
        targetObj.setPassWdMd(srcObj.getPassWdMd());//passWdMd
//        targetObj.setPassWd(srcObj.getPassWd());//passWd
        targetObj.setEmail(srcObj.getEmail());//用户邮箱
        targetObj.setUserStatus(srcObj.getUserStatus());
        targetObj.setLoginIp(srcObj.getLoginIp());//最后登录IP
        targetObj.setLoginDate(srcObj.getLoginDate());//最后登录时间
        targetObj.setCreateTime(srcObj.getCreateTime());//创建时间
        targetObj.setUpdateTime(srcObj.getUpdateTime());//最近修改时间
        targetObj.setDelFlag(srcObj.getDelFlag());//是否删除（0 未删除，1 已删除）
        targetObj.setDeptName(srcObj.getDeptName());//部门名称
        targetObj.setRemark(srcObj.getRemark());
        targetObj.setSex(srcObj.getSex());
        return targetObj;
    }

    public static List<SysUserBO> doList2BOList(List<SysUserDO> doList) {
        List<SysUserBO> boList = new ArrayList<>();
        if (CollectionUtils.isEmpty(doList)) return boList;

        doList.stream().forEach(d -> boList.add(SysUserBeanUtil.sysUserDOToBO(d)));
        return boList;
    }

    public static List<SysUserDO> boList2DOList(List<SysUserBO> boList) {
        List<SysUserDO> doList = new ArrayList<>();
        if (CollectionUtils.isEmpty(boList)) return doList;

        boList.stream().forEach(b -> doList.add(SysUserBeanUtil.sysUserBOToDO(b)));
        return doList;
    }

    public static void saveCheckParam(SysUserBO userBO) {
        if (userBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isBlank(userBO.getUserName())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isBlank(userBO.getLoginNo())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (userBO.getSysDeptId() == null) {
            throw new AppException("请选择用户所属部门");
        }
        if (userBO.getSysRoleId() == null) {
            throw new AppException("请为该用户分配角色");
        }
        if (userBO.getUserStatus() == null) {
            userBO.setUserStatus(SystemConstant.STR_0);
        }
        if (StringUtils.isBlank(userBO.getPassWd())) {
            userBO.setPassWd(SystemConstant.DEFAULT_PASSWD);
        }
    }
}